package com.zh.www.dnskeep.domain;

import com.alibaba.fastjson.JSONObject;
import com.zh.www.utils.lang.StringUtils;
import com.zh.www.utils.network.HttpUtils;
import com.zh.www.utils.network.IPUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author zh
 * @version 1.0
 * @date 2020/6/9 16:23
 * @email 690378361@163.com
 */
@Component
public class DNSSetting {

    private static final String key ="dns-IpV4";
    private static final String tokenKey = "dns-token";
    private static final String tokenUrl = "https://iam.myhuaweicloud.com/v3/auth/tokens?nocatalog=true";
    private static final JSONObject jsonObject = JSONObject.parseObject("{\"auth\":{\"identity\":{\"methods\":[\"password\"],\"password\":{\"user\":{\"domain\":{\"name\":\"hw55410709\"},\"name\":\"hw55410709\",\"password\":\"zhanghao.sdc\"}}},\"scope\":{\"project\":{\"name\":\"cn-south-1\"}}}}");
    private static final String blogDataId = "8aace3b971887e9f0171915721e450d4";
    private static final String zoneId = "8aace3ba718880fe01718b90459d678d";
    private static final String dataDataId = "8aace3b9728536c10172982cb03a09ae";
    private static final String wwwDataId = "8aace3b9719822e301719b24edae6561";
    private static final String dnsMdUrl =  "https://dns.cn-south-1.myhuaweicloud.com/v2/zones/{}/recordsets/{}";
    private static final String dnsReqParam = "{\"description\":\"Thisisanexamplerecordset.\",\"records\":[\"{}\"],\"ttl\":300}";
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Scheduled(cron = "0 0/10 * * * ?")
    public void setting(){
        String ipv4 = IPUtils.getV4IP();

        String value = stringRedisTemplate.opsForValue().get(key);
        if(StringUtils.isEmpty(value) || !ipv4.equals(value)){
            stringRedisTemplate.opsForValue().set(key, ipv4,60*10*1000, TimeUnit.SECONDS);
            value = ipv4;
            CloseableHttpResponse result =   HttpUtils.doPostJsonRtResponse(tokenUrl,jsonObject);
            String token = result.getLastHeader("X-Subject-Token").getValue();
            String redisToken = stringRedisTemplate.opsForValue().get(tokenKey);
            if(StringUtils.isEmpty(redisToken)|| !redisToken.equals(token)){
                stringRedisTemplate.opsForValue().set(tokenKey, token,60*10*1000, TimeUnit.SECONDS);
                String dnsNewUrl = dnsMdUrl.replaceFirst("\\{\\}",zoneId).replaceFirst("\\{\\}",blogDataId);
                String requestParam = dnsReqParam.replaceFirst("\\{\\}",value);
                HttpUtils.doPut(dnsNewUrl,redisToken,requestParam);
                dnsNewUrl = dnsMdUrl.replaceFirst("\\{\\}",zoneId).replaceFirst("\\{\\}",dataDataId);
                HttpUtils.doPut(dnsNewUrl,redisToken,requestParam);
                dnsNewUrl = dnsMdUrl.replaceFirst("\\{\\}",zoneId).replaceFirst("\\{\\}",wwwDataId);
                HttpUtils.doPut(dnsNewUrl,redisToken,requestParam);
            }

        }



    }

    public static void main(String[] args) {
        String ipv4 = IPUtils.getV4IP();
        CloseableHttpResponse result =   HttpUtils.doPostJsonRtResponse(tokenUrl,jsonObject);
        String token = result.getLastHeader("X-Subject-Token").getValue();
        String dnsNewUrl = dnsMdUrl.replaceFirst("\\{\\}",zoneId).replaceFirst("\\{\\}",blogDataId);
        String requestParam = dnsReqParam.replaceFirst("\\{\\}",ipv4);
        HttpUtils.doPut(dnsNewUrl,token,requestParam);
    }

}
